package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.User;
import com.qf.entity.UserVO;
import com.qf.service.UserService;
import com.qf.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author Qiao
 * @date 2025/8/11
 * @desc
 */
@RestController
@Api(tags = "用户表的增删改查和一对多的多表联查演示")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/user/search/v2")
    @ApiOperation(value = "将前端传来的数据存到map中进行模糊查询")
    public R userSelect(@RequestParam HashMap<String,String> map){
        System.out.println("前端发来的搜索关键词" + map);
        List<User> list = userService.searchByMap(map);
        return R.success(list);
    }


    @GetMapping("/user/delete/batch")
    @ApiOperation(value = "将前端传的id存到list集合中再根据集合中的元素进行批量删除")
    private R deleteBatch(@RequestParam List<Integer> ids){
        System.out.println("批量删除,接收到前端的id:" + ids);
        userService.deleteBatch(ids);
        return R.success();
    }

    @GetMapping("/user/update/v2")
    @ApiOperation("根据前端传来的数据对user表进行更新")
    private R userUpdate(User user){
        System.out.println("前端发送过来的更新数据:" + user);
        boolean isOk = userService.updateById(user);

        return R.success(isOk);
    }


    // 一对多
    @GetMapping("/user/orders/{id}")
    @ApiOperation(value = "一对多多表联查演示")
    private R findUserWithOrdersById(@PathVariable int id){

       UserVO userVO = userService.findUserWithOrdersById(id);
       return R.success(userVO);
    }



    // 分页查询
    @GetMapping("/user/list/page")
    @ApiOperation(value = "分页查询")
    private R userList(int pageSize,int pageNum){
        // 开始分页
        PageHelper.startPage(pageNum,pageSize);

        List<User> list = userService.list();

        PageInfo<User> pageInfo = new PageInfo<>(list);
        System.out.println("pageInfo.getPages() = " + pageInfo.getPages());
        System.out.println("pageInfo.getTotal() = " + pageInfo.getTotal());
        System.out.println("pageInfo.getList() = " + pageInfo.getList());
        int[] navigatepageNums = pageInfo.getNavigatepageNums();
        System.out.println("navigatepageNums = " + Arrays.toString(navigatepageNums));
        return R.success();
    }
}
